package com.qfedu.vhr.employee.config;

import com.qfedu.vhr.employee.entity.MailSendLog;
import com.qfedu.vhr.employee.service.IMailSendLogService;
import jakarta.annotation.PostConstruct;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * @author baize
 * @date 2023/2/8
 * @site www.qfedu.com
 */
@Configuration
public class RabbitTemplateConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback {

    @Autowired
    IMailSendLogService mailSendLogService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init() {
        rabbitTemplate.setReturnsCallback(this);
        rabbitTemplate.setConfirmCallback(this);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        if (b) {
            //说明发送成功
            String id = correlationData.getId();
            //修改消息状态
            MailSendLog log = new MailSendLog();
            log.setMsgId(id);
            log.setStatus(1);
            mailSendLogService.updateById(log);
        }
    }

    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {

    }
}
